package com.example.messagemodule.ui;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.examination.mlib.baseold.ModuleBaseActivity;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.AllStringConstants;
import com.examination.mlib.constants.ClickApi;
import com.examination.mlib.customview.surface.SurfaceVideo;
import com.examination.mlib.interfaceapi.NetWorkCallBack;
import com.example.messagemodule.R;
import com.example.messagemodule.adapter.GuideAdapter;
import com.example.messagemodule.entity.GuideVideoEntity;
import com.google.gson.Gson;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.CommonUtils;
import com.yilijk.base.utils.DevicesUtils;

import org.json.JSONObject;

import java.util.ArrayList;

import crossoverone.statuslib.StatusUtil;

@Route(path = ARouterConstants.message.GuideVideoActivity)
public class GuideVideoActivity extends ModuleBaseActivity {
    private SurfaceVideo sv;

    @Autowired
    protected String commodityId;
    @Autowired
    protected String commodityType;


    @Override
    public int bondLayout() {
        return R.layout.activity_guide_video;
    }

    ArrayList<GuideVideoEntity.DataBean> videos;
    GuideAdapter guideAdapter;
    Handler handler;

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void initView() {
        ClickUtils.sendEvent(ClickApi.Message.page_guidevideo,this,"使用说明载入");

        handler = new Handler();
        StatusUtil.setSystemStatus(this, false, true);
        StatusUtil.setUseStatusBarColor(this, Color.WHITE);


        TextView titleBarTitle = findViewById(R.id.title_bar_title);
        ImageView titleBarBack = findViewById(R.id.title_bar_back);

        titleBarTitle.setText("使用说明");
        sv =  findViewById(R.id.sv);
        RecyclerView rv = findViewById(R.id.rv);

        if (TextUtils.isEmpty(commodityId) || TextUtils.isEmpty(commodityType)){
            finish();
        }
        rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        videos = new ArrayList<>();
        guideAdapter = new GuideAdapter(videos);
        rv.setAdapter(guideAdapter);
        guideAdapter.notifyDataSetChanged();
        RequestParams params = new RequestParams();
        params.put("commodityId",commodityId);
        params.put("commodityType",commodityType);
        HttpUtils.get(AllStringConstants.MessageUrl.GetCommodityVideo, params, new HttpCallBack<GuideVideoEntity>() {
            @Override
            public void onSuccess(GuideVideoEntity guideVideoEntity, int i) {
                if (guideVideoEntity!=null && guideVideoEntity.getData()!=null){
                    videos.clear();
                    videos.addAll(guideVideoEntity.getData());
                    guideAdapter.notifyDataSetChanged();
                    handler.post(() -> {
                        if (guideVideoEntity.getData().size()>0){
                            if (guideVideoEntity.getData().get(0).getOssVideoUrl()!=null){
                                sv.setVideoUrl(guideVideoEntity.getData().get(0).getOssVideoUrl());
                                sv.initSurface(true);
                            }
                        }
                    });
                }
            }

            @Override
            public void onFailure(String s, int i) {

            }
        });

        guideAdapter.setOnItemClickListener((adapter, view, position) -> {
            ClickUtils.sendEvent(ClickApi.Message.click_videonext,this,"播放下一条点击");
            sv.reset();
            sv.setVideoUrl(videos.get(position).getOssVideoUrl());
            sv.initSurface(true);
            guideAdapter.p = position;
            guideAdapter.notifyDataSetChanged();
        });
        titleBarBack.setOnClickListener(v -> {
            sv.reset();
            finish();
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        sv.setP(data != null ? data.getIntExtra("currentPosition", 0) : 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sv.clear();
    }

}